import helper
from collections import defaultdict
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize

import comparison as cmpr

class relevanceFeedback:
    def __init__(self, query, list_document_relevance, list_document_irrelevance, inverted_file_location, comparison):
        self.query = query
        self.list_document_relevance = list_document_relevance
        self.list_document_irrelevance = list_document_irrelevance
        self.inverted_file_location = inverted_file_location
        self.cmpr_instance = comparison

    def calculate(self, method, beta=1 , gamma=1):
        
        #bentuk inverted file dalam defaultdict
        data_dict = defaultdict(list)
        
        f = open(self.inverted_file_location, 'r')

        for line in f:
            token = line.split()
            word = token[0]
            doc_id = token[1]
            raw_tf = token[2]
            df = token[3]
            weight = token[4]
            tupple = (doc_id, raw_tf, df, weight)
            data_dict[word].append(tupple)

        f.close()
        
	#print "data_dict : ", data_dict
	
        relevance_dictionary = {}
        irrelevance_dictionary = {}
        
        inv_file_list = {}
        
        print method
        
        #untuk tiap kata dalam query ambil bobot kata nya dari tiap dokumen relevan
        #jumlahkan dan bagi jumlah dokumen relevan
        
        #tokenisasi kata tiap query
        #query_token = word_tokenize(self.query);
        #query_token = queryProcessing
        #print query_token
        
        #queryprocessing here
        q0 = self.cmpr_instance.createWeightDict(self.query)
        print "q0 : ",q0
        
        query_token = []
        for key in q0:
            query_token.append(key)
        #print "query token : ", query_token
        for key in query_token:
            if (key in data_dict):
                weight_relevant = 0.0
                for y in self.list_document_relevance:
                        for z in range(len(data_dict[key])):
                            if (data_dict[key][z][0] == str(y)):
                                weight_relevant += float(data_dict[key][z][3])
				print "weight_relevant : ", weight_relevant
                        relevance_dictionary[key] = weight_relevant;
        
        #print relevance_dictionary
        
        for key in query_token:
            if (key in data_dict):
                weight_irrelevant = 0.0
                if (method != 'dechi'):
                    for y in self.list_document_irrelevance:
                            for z in range(len(data_dict[key])):
                                if (data_dict[key][z][0] == str(y)):
                                    weight_irrelevant += float(data_dict[key][z][3])
                            irrelevance_dictionary[key] = weight_irrelevant;
                else:
                    if (len(data_dict[key]) > 0):
                        weight_irrelevant = float(data_dict[key][0][3])
                    irrelevance_dictionary[key] = weight_irrelevant
        
        #print irrelevance_dictionary
        
        #untuk tiap kata dalam query ambil bobot kata nya dari tiap dokumen relevan
        #jumlahkan dan bagi jumlah dokumen relevan
        
        q1 = {}
        
        for a in q0:
            if (method == 'rocchio'):
                q1[a] = q0[a] + relevance_dictionary[a]/len(self.list_document_relevance) - irrelevance_dictionary[a]/len(self.list_document_irrelevance)
            else:
                q1[a] = q0[a] + relevance_dictionary[a] - irrelevance_dictionary[a]
        
        print "q1 : ", q1, '\n'
        
        return self.cmpr_instance.reCalcAllRank(q1, self.cmpr_instance.calcDocumentVector(), 0.0)


#testing
#query = "red big car"
#list_doc_rel = ['2','3']
#list_doc_irrel = ['1']
#q0 = { "red": 0.176, "big": 0.447 , "car": 0.176}
#inv_file_location = "inverted_file"
#
##nanti query jadi ga butuh
#r = relevanceFeedback(query, list_doc_rel, list_doc_irrel, inv_file_location)
##nanti q0 diapus
#r.calculate(q0, 'rocchio', 1, 1)
#r.calculate(q0, 'ide', 1, 1)
#r.calculate(q0, 'dechi', 1, 1)
